package com.kly.chatter.api.application.service.impl;

import com.aliyun.openservices.shade.io.netty.util.internal.StringUtil;
import com.google.common.collect.Maps;
import com.kly.chatter.api.application.controller.chatter.req.MessageNoticeReq;
import com.kly.chatter.api.application.controller.chatter.req.MessageVipNoticeReq;
import com.kly.chatter.api.application.service.AsyncSendMsgService;
import com.kly.chatter.api.application.service.ChatterImRewardService;
import com.kly.chatter.db.dao.ChatterImConfigDao;
import com.kly.chatter.db.dao.ChatterImRewardDao;
import com.kly.chatter.db.entity.ChatterImConfigEntity;
import com.kly.chatter.db.entity.ChatterImRewardEntity;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.service.MemberService;
import com.kly.user.chatterService.ChatterUserService;
import com.kly.user.dto.chatter.response.UserChatterDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ChatterImRewardServiceImpl implements ChatterImRewardService {

    @Autowired
    private ChatterImRewardDao chatterImRewardDao;

    @Autowired
    private ChatterImConfigDao chatterImConfigDao;

    @Resource
    private MemberService memberService;

    @Autowired
    private ChatterImRewardService chatterImRewardService;

    @Resource
    AsyncSendMsgService asyncSendMsgService;

    @Resource
    ChatterUserService chatterUserService;

    @Override
    public Boolean haveChatterImReward(ChatterImRewardEntity chatterImRewardEntity) {
        final HashMap<String, Object> params = Maps.newHashMap();
        params.put("chatterCode", chatterImRewardEntity.getChatterCode());
        params.put("modelCode", chatterImRewardEntity.getModelCode());
        params.put("userCode", chatterImRewardEntity.getUserCode());
        params.put("rewardDate", chatterImRewardEntity.getRewardDate());
        params.put("configId", chatterImRewardEntity.getConfigId());
        List<ChatterImRewardEntity> chatterImRewardEntities = chatterImRewardDao.selectByParams(params);
        return !chatterImRewardEntities.isEmpty();
    }

    @Override
    public void add(ChatterImRewardEntity chatterImRewardEntity) {
        chatterImRewardDao.insert(chatterImRewardEntity);
    }

    @Override
    public void sendMessageNotice(MessageNoticeReq messageNoticeReq) {
        List<ChatterImConfigEntity> chatterImConfigEntities = chatterImConfigDao.selectAll();
        Boolean response = RpcUtils.getResult(memberService.ifMember(messageNoticeReq.getUserCode()));
        int ifMember = response ? 1 : 0;
        List<ChatterImConfigEntity> configEntities = chatterImConfigEntities.stream().filter(s -> null != s && s.getVip() == ifMember).collect(Collectors.toList());
        if (!configEntities.isEmpty()) {
            for (int i = 0; i < configEntities.size(); i++) {
                ChatterImConfigEntity chatterImConfigEntity = configEntities.get(i);
                if (messageNoticeReq.getMessageCount() == (chatterImConfigEntity.getStartCount() + 1)) {

                    //发送通知
                    if (chatterImConfigEntity.getReward().compareTo(BigDecimal.ZERO) > 0) {
                        String message = String.format("Congratulations, you've earned a $%s reward!\uD83C\uDF89", chatterImConfigEntity.getReward());
                        List<String> messageList = new ArrayList<>();
                        messageList.add("$" + chatterImConfigEntity.getReward());
                        asyncSendMsgService.sendMsg(messageNoticeReq.getUserCode(), "MESSAGE", messageNoticeReq.getModelUserCode(), "RC:InfoNtf", message, message, messageNoticeReq.getChatterCode(), 1, messageList);

                        if (i != (configEntities.size() - 1)) {
                            ChatterImConfigEntity config = configEntities.get(i + 1);
                            List<String> messageList1 = new ArrayList<>();
                            messageList1.add("$" + config.getReward());
                            message = String.format("Keep chatting with users and you can get $%s bonus!", config.getReward());
                            asyncSendMsgService.sendMsg(messageNoticeReq.getUserCode(), "MESSAGE", messageNoticeReq.getModelUserCode(), "RC:InfoNtf", message, message, messageNoticeReq.getChatterCode(), 1, messageList1);
                        }
                    }
                } else if (chatterImConfigEntity.getEndCount() != null && messageNoticeReq.getMessageCount() > (chatterImConfigEntity.getStartCount() + 1)) {
                    int limitCount = (chatterImConfigEntity.getEndCount() * chatterImConfigEntity.getRangeRate()) / 100;
                    if (messageNoticeReq.getMessageCount() == limitCount) {
                        //发送通知
                        if (i != (configEntities.size() - 1)) {
                            ChatterImConfigEntity config = configEntities.get(i + 1);
                            int lastCount = (chatterImConfigEntity.getEndCount() - limitCount) + 1;
                            List<String> messageList = new ArrayList<>();
                            messageList.add(String.valueOf(lastCount));
                            messageList.add("$" + config.getReward());
                            String message = String.format("User sends %s more messages and you get rewarded with $%s!", lastCount, config.getReward());
                            asyncSendMsgService.sendMsg(messageNoticeReq.getUserCode(), "MESSAGE", messageNoticeReq.getModelUserCode(), "RC:InfoNtf", message, message, messageNoticeReq.getChatterCode(), 1, messageList);
                        }
                    }
                }
            }
        }
    }

    @Override
    public void sendVipMessageNotice(MessageVipNoticeReq messageVipNoticeReq) {
        if (messageVipNoticeReq.getType() != 1) {
            String message = "The current user session has reached the limit, as soon as possible to remind users to become a VIP.";
            if (StringUtil.isNullOrEmpty(messageVipNoticeReq.getChatterCode())) {
                UserChatterDto userChatterDto = RpcUtils.getResult(chatterUserService.getChatter(messageVipNoticeReq.getModelUserCode()));
                if (userChatterDto != null) {
                    messageVipNoticeReq.setChatterCode(userChatterDto.getChatterUserCode());
                }
            }
            asyncSendMsgService.sendMsg(messageVipNoticeReq.getUserCode(), "MESSAGE", messageVipNoticeReq.getModelUserCode(), "RC:InfoNtf", message, message, messageVipNoticeReq.getChatterCode(), 2, null);
        }
    }

    @Override
    public BigDecimal getImTotalReward(String chatterCode, Integer rewardDate) {
        return chatterImRewardDao.getImTotalReward(chatterCode, rewardDate);
    }

    @Override
    public ChatterImRewardEntity getLastRecord(String chatterCode, String modelCode, String userCode, Integer rewardDate) {
        return chatterImRewardDao.getLastRecord(chatterCode, modelCode, userCode, rewardDate);
    }


}
